<?php

namespace app\common\controller;

use think\Controller;

/**
 * 前台公共控制器
 */
class Home extends Controller
{
    /**
     * redirect 重定向跳转参数
     */
    public function __construct($redirect = true)
    {
        parent::__construct();
        $this->BusinessModel = model('Business.Business');

        //调用判断是否需要登录
        $this->IsLogin($redirect);
    }

    /**
     * 判断是否有登录
     * redirect = true 默认要跳转
     * redirect = false 默认不跳转，并返回登录人的信息
     */
    public function IsLogin($redirect = true)
    {
        //获取cookie信息
        $business = cookie('business') ? cookie('business') : [];

        if (empty($business)) {
            if ($redirect) {
                $this->error('请先登录', url('home/index/login'));
                exit;
            } else {
                return false;
            }

        }

        //获取出cookie选项
        $id = isset($business['id']) ? $business['id'] : 0;
        $mobile = isset($business['mobile']) ? $business['mobile'] : 0;

        $where = [
            'id' => $id,
            'mobile' => $mobile
        ];

        //查询客户是否存在
        $business = $this->BusinessModel->where($where)->find();

        if (empty($business)) {
            //删除cookie 清空cookie
            cookie('business', null);

            if ($redirect) {
                $this->error('非法登录，请重新登录', url('home/index/login'));
                exit;
            } else {
                return false;
            }
        }
        
        //赋值
        $this->assign('login', $business);
        return $business;
    }


}